class Table {//游戏卓对象。管理：4个玩家、奖金池、玩家选项后的操作
//构造函数
constructor(me:Player){ players.add(me) }
    //成员属性
    var pool = 0     //奖金池
    val players: ArrayList<Player> = arrayListOf()   //四个玩家顺序
    var running = true  //本局游戏是否在进行
    var number:Int = 0  //下注的金额

    //成员函数
    fun first(choice:Int){
        players.add(Player("史珍香"))
        players.add(Player("郝仁"))
        players.add(Player("达椿竹"))

        for(play in players)
            println(play.name+"进入房间")
        players.shuffle()   //打乱座位顺序
        when(choice){
            1->{ this.pool = 100*4
                for (i in 0..3){
                    this.players[i].balance-=100
                    this.players[i].putIn+=100
                } }
            2->{this.pool = 200*4
                for (i in 0..3){
                    this.players[i].balance-=200
                    this.players[i].putIn+=200
                }}
            3->{this.pool = 500*4
                for (i in 0..3){
                    this.players[i].balance-=500
                    this.players[i].putIn+=500
                }}
        }
    }
    fun doNext(choice: Int, nowPlayer: Player, manager: PokerM){ //执行游戏中玩家选项的操作
        when(choice){
            1->{ //下注，奖金池+，玩家putIn+，玩家balance-
                println("您的余额为：$${nowPlayer.balance}")
                print("请输入下注金额：$")
                number = readln().toInt()
                nowPlayer.balance-=number
                nowPlayer.putIn+=number
                pool+=number
                println("成功下注$$number  \n您的余额为：$${nowPlayer.balance}  当前奖金池：$$pool")
            }
            2->{//跟注，奖金池+，玩家putIn+，玩家balance-
                nowPlayer.balance-=number
                nowPlayer.putIn+=number
                pool+=number
                println("成功跟注$$number  \n您的余额为：$${nowPlayer.balance}  当前奖金池：$$pool")
            }
            3->{//弃牌，奖金池-，玩家putIn-，玩家balance+
                nowPlayer.balance+=nowPlayer.putIn
                pool-=nowPlayer.putIn
                nowPlayer.putIn=0
                println("弃牌成功！\n您的余额为：$${nowPlayer.balance}  当前奖金池：$$pool")
                nowPlayer.exists = false
            }
            4->{//all-in,奖金池+，玩家putIn+，玩家balance-
                pool+=nowPlayer.balance
                nowPlayer.putIn+=nowPlayer.balance
                nowPlayer.balance=0
                println("成功all-in\n您的余额为：$0 当前奖金池：$$pool")
                //开始比牌
                compare(nowPlayer,manager)
            }
            5->{//比牌
                compare(nowPlayer,manager)
            }
        }
    }

    fun compare(nowPlayer: Player, manager: PokerM) {
        for (i in 0..3){
            if (players[i].exists && players[i]!=nowPlayer){
                print("${i+1}.${players[i].name}   ") //输出未弃牌的对手序号和名字
            }
        }
        print("请选择比牌对象：")
        val selectPlayer = players[readln().toInt()-1]
        if (manager.comparePokers(nowPlayer.poker,selectPlayer.poker)){
            println("\n恭喜你获得本局胜利！")
            nowPlayer.balance+=pool
            pool=0
            println("您的余额为：$${nowPlayer.balance}")
        }else{
            println("\n比拼失败")
            selectPlayer.balance+=pool
            pool=0
        }
        close()  //比完牌，结束本局
    }

    fun close(){//本局结束
        for (i in 0..3) players[i].putIn=0 //关闭时，所有玩家putIn清零
        number = 0
        println("游戏结束")
        running = false // boolean 结束本局
    }
}